package com.aabte.spring.condition;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @author Daniel
 */
public class LinuxCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        // 通过ioc获取BeanFactory
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        // 获取类加载器
        ClassLoader classLoader = context.getClassLoader();
        // 获取当前环境信息
        BeanDefinitionRegistry registry = context.getRegistry();
        // 获取到Bean定义的注册类
        ResourceLoader resourceLoader = context.getResourceLoader();
        // 获取到环境变量
        Environment environment = context.getEnvironment();

        // 可以在这里判断容器中是否有某个Bean对象

        String osName = environment.getProperty("os.name");
        if (osName.contains("Linux")) {
            return true;
        } else {
            return false;
        }
    }

}
